﻿//========================================================= 
/**@file ILogCat.h
 * @brief 日志封装的接口类
 * 
 * @date 2018-06-19   18:30:29
 * @author Zhyioo 
 * @version 1.0
 */ 
//--------------------------------------------------------- 
#ifndef _LIBZHOUYB_ILOGCAT_H_
#define _LIBZHOUYB_ILOGCAT_H_
//--------------------------------------------------------- 
#include "../../include/Base.h"
//--------------------------------------------------------- 
namespace zhou_yb {
namespace extension {
namespace logcat {
//--------------------------------------------------------- 
/// 日志级别
enum LogLevel
{
    LogAll = 0,
    LogTrace,
    LogDebug,
    LogInfo,
    LogWarning,
    LogError,
    LogFatal,
    LogOff
};
//--------------------------------------------------------- 
/// 日志输出器
struct ILogAppender
{
    /// 记录一条日志
    virtual void append(LogLevel lv, const char* tag, const char* str) = 0;
    
    virtual ~ILogAppender() {}
};
/// 日志布局
struct ILogLayout
{
    /// 将日志级别输出为字符串
    static const char* LevelToString(LogLevel lv)
    {
        switch(lv)
        {
        case LogAll:
            return "ALL  ";
        case LogTrace:
            return "TRACE";
        case LogDebug:
            return "DEBUG";
        case LogInfo:
            return "INFO ";
        case LogWarning:
            return "WARN ";
        case LogError:
            return "ERROR";
        case LogFatal:
            return "FATAL";
        default:
            break;
        }
        return "OFF  ";
    }

    /// 获取布局信息
    virtual size_t message(LogLevel lv, const char* tag, string& msg) = 0;
    
    virtual ~ILogLayout() {}
};
//--------------------------------------------------------- 
/// 默认的日志追随器构造函数定义
#define LogCatFollowerConstructor(FollowerType) \
    FollowerType() {} \
    FollowerType(const Ref<ILogAppender>& appender, LogLevel lv, const char* tag) : \
        LogCatFollower(appender, lv, tag) {} \
    FollowerType(const FollowerType& obj) \
    { \
        _copy(*(const_cast<FollowerType*>(&obj))); \
    }
//--------------------------------------------------------- 
/// 日志跟随器封装
class LogCatFollower : public RefObject
{
protected:
    Ref<ILogAppender> _appender;
    LogLevel _lv;
    const char* _tag;

    void _copy(LogCatFollower& obj)
    {
        _appender = obj._appender;
        obj._appender.Free();

        _lv = obj._lv;
        _tag = obj._tag;
    }
public:
    LogCatFollower() {}
    LogCatFollower(const Ref<ILogAppender>& appender, LogLevel lv, const char* tag) : 
        _appender(appender), _lv(lv), _tag(tag) {}
    LogCatFollower(const LogCatFollower& obj)
    {
        _copy(*(const_cast<LogCatFollower*>(&obj)));
    }

    inline bool IsEnable() const
    {
        return !_appender.IsNull();
    }
    void Append(const string& text)
    {
        if(IsEnable())
        {
            _appender->append(_lv, _tag, text.c_str());
        }
    }
};
//--------------------------------------------------------- 
} // namespace logcat
} // namespace extension
} // namespace zhou_yb
//--------------------------------------------------------- 
#endif // _LIBZHOUYB_ILOGCAT_H_
//========================================================= 
